package com.example.login.Controller;

import com.example.login.Mapper.GoodsMap;
import com.example.login.Pojo.*;
import com.example.login.Service.SearchGoodsService;
import com.example.login.Service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import java.util.List;


@Controller
public class ShopController {
    @Autowired
    SearchGoodsService sg;
    @RequestMapping(value={"/"})
     public String index(Model model, HttpSession session)
    {
//        session.setAttribute("user",new User(1,"xchhh","ff"));
        ArrayList<ArrayList<Goods>> goodList=sg.allGoods(false);
        model.addAttribute("goods",goodList);
        String search=" ";
        model.addAttribute("searchWords",search);
        return "商城首页";
    }
    @RequestMapping("/shopmobile")
    public String shopMobile(Model model){
        ArrayList<ArrayList<Goods>> goodList=sg.mobileGoods(false);
        model.addAttribute("goods",goodList);
        model.addAttribute("cid","手机");
        String search=" ";
        model.addAttribute("searchWords",search);
        return "商城首页";
    }
    @RequestMapping("/shopcomputer")
    public String shopComputer(Model model)
    {
        ArrayList<ArrayList<Goods>> goodList=sg.PCGoods(false);
        model.addAttribute("goods",goodList);
        model.addAttribute("cid","电脑");
        String search=" ";
        model.addAttribute("searchWords",search);
        return "商城首页";
    }
    @RequestMapping("/shopmp3")
    public String shopMp3(Model model)
    {
        ArrayList<ArrayList<Goods>> goodList=sg.MP3Goods(false);
        model.addAttribute("goods",goodList);
        model.addAttribute("cid","MP3");
        String search=" ";
        model.addAttribute("searchWords",search);
        return "商城首页";
    }
    @Autowired
    SearchService se;
    @RequestMapping("/shop")
    public String search(@RequestParam ("search")String search, Model model)
    {
        System.out.println(search);
        ArrayList<ArrayList<Goods>> goodList =se.searchGoods(search);
        model.addAttribute("goods",goodList);
        model.addAttribute("searchWords",search);
        return "商城首页";
    }
    //按销量排序
    @RequestMapping("/shopsort/{cid}")
    public String sort(@PathVariable("cid") String cid, Model model)
    {
        ArrayList<ArrayList<Goods>> goodList;
        System.out.println(cid);
        if(cid.equals("null"))
        {
            goodList=sg.allGoods(true);
            model.addAttribute("goods",goodList);
            return "商城首页";
        }
        switch (cid)
        {
            case "手机":
                goodList=sg.mobileGoods(true);
                model.addAttribute("goods",goodList);
                break;
            case "电脑":
                goodList=sg.PCGoods(true);
                model.addAttribute("goods",goodList);
                break;
            case "MP3":
                goodList=sg.MP3Goods(true);
                model.addAttribute("goods",goodList);
                break;
        }
        model.addAttribute("cid",cid);
        String search=" ";
        model.addAttribute("searchWords",search);
        return "商城首页";
    }
    //详情页
    @Autowired
    GoodsMap goodsMap;
    @RequestMapping("/item/{id}")
    public String item(@PathVariable("id") int id,Model model)
    {
        String cid = goodsMap.determine(id);
        String sku_code="a";
        model.addAttribute("sku",sku_code);

        switch (cid)
        {
            case "MobilePhone":
                List<mobileGoods> mobileGoods= goodsMap.getMobileGoodsById(id);
                model.addAttribute("mobiles",mobileGoods);
                return "商品详情/手机商品详情";
            case "MP3":
                List<mp3Goods> mp3Goods =goodsMap.getMp3GoodsById(id);
                model.addAttribute("mp3s",mp3Goods);
                return "商品详情/MP3商品详情";
            case "PC":
                List<PCGoods> pcGoods=goodsMap.getPCGoodsById(id);
                System.out.println(pcGoods);
                model.addAttribute("pcs",pcGoods);
                return "商品详情/电脑商品详情";
            default:
                return "/error";
        }
    }
}
